package com.mrd.shiptask_shipowner.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;

import com.mrd.shiptask_shipowner.R;
import com.mrd.shiptask_shipowner.activity.ConfirmOrderActivity;
import com.mrd.shiptask_shipowner.activity.ElectronManifestActivity;
import com.mrd.shiptask_shipowner.constants.database.DBUtil;
import com.mrd.shiptask_shipowner.serviecs.initdatadao.InitDatasDaoImpl;
import com.mrd.shiptask_shipowner.unit_tools.DatasTools;
import com.mrd.shiptask_shipowner.unit_tools.FunctionOfActivity;
import com.mrd.shiptask_shipowner.unit_tools.flesh_listview.MyListView;
import com.mrd.shiptask_shipowner.unit_tools.flesh_listview.MyListView.OnRefreshListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ConfirmOrderCompletedFragment extends Fragment {
	private List<Map<String, Object>> mData;
	private MyAdapter adapter;
	private MyListView listView2;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(
				R.layout.electron_mainifest_completed_main, container, false);
		setInitView(view);
		setInitDatas(view);
		return view;
	}

	private void setInitView(View view) {
		// TODO Auto-generated method stub

	}

	public void setInitDatas(View view) {
		initListView(view);
	}

	private void initListView(View view) {
		// TODO Auto-generated method stub
		try {
			// true为已经完成的订单，false为未完成的订单
			JSONArray jas = DBUtil.selectConfirmOrderTable(getActivity(), true);
			List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < jas.length(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				String getId = DatasTools.getJsonValue(jas.getJSONObject(i),
						"ID");
				String getCode = DatasTools.getJsonValue(jas.getJSONObject(i),
						"CODE");
				String getShipper = DatasTools.getJsonValue(
						jas.getJSONObject(i), "SHIPPER");
				String getGoods = DatasTools.getJsonValue(jas.getJSONObject(i),
						"GOODS");
				String getGoodsCount = DatasTools.getJsonValue(
						jas.getJSONObject(i), "GOODS_COUNT");
				String getGoodsUnit = DatasTools.getJsonValue(
						jas.getJSONObject(i), "GOODS_UNIT");
				String getEndPort = DatasTools.getJsonValue(
						jas.getJSONObject(i), "DISCHARGE_PORT");
				String getStartPort = DatasTools.getJsonValue(
						jas.getJSONObject(i), "LOADING_PORT");
				String getClossingDate = DatasTools.getJsonValue(
						jas.getJSONObject(i), "CLOSSING_DATE");
				String getLoadingPort = DatasTools.getJsonValue(
						jas.getJSONObject(i), "LOADING_PORT");
				String getDischargePort = DatasTools.getJsonValue(
						jas.getJSONObject(i), "DISCHARGE_PORT");
				String getLoadingCount = DatasTools.getJsonValue(
						jas.getJSONObject(i), "LOADING_COUNT");
				String getDischarge_Count = DatasTools.getJsonValue(
						jas.getJSONObject(i), "DISCHARGE_COUNT");
				String getShipperConfirm = DatasTools.getJsonValue(
						jas.getJSONObject(i), "SHIPPER_CONFIRM");
				String getFinish = DatasTools.getJsonValue(
						jas.getJSONObject(i), "FINISH");
				String getShipperConfirmAmount = DatasTools.getJsonValue(
						jas.getJSONObject(i), "SHIPPER_CONFIRM_AMOUNT");
				map.put("getId", getId);
				map.put("getCode", getCode);
				map.put("getShipper", getShipper);
				map.put("getGoods", getGoods);
				map.put("getGoodsCount", getGoodsCount);
				map.put("getGoodsUnit", getGoodsUnit);
				map.put("getEndPort", getEndPort);
				map.put("getStartPort", getStartPort);
				map.put("getClossingDate", getClossingDate);
				map.put("getLoadingPort", getLoadingPort);
				map.put("getDischargePort", getDischargePort);
				map.put("getLoadingCount", getLoadingCount);
				map.put("getDischarge_Count", getDischarge_Count);
				map.put("getShipperConfirm", getShipperConfirm);
				map.put("getFinish", getFinish);
				map.put("getShipperConfirmAmount", getShipperConfirmAmount);
				mapList.add(map);
			}

			listView2 = (MyListView) view.findViewById(R.id.listView2);
			// 生成动态数组，并且转入数据
			mData = mapList;
			// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
			adapter = new MyAdapter(getActivity());
			listView2.setAdapter(adapter);
			listView2.setonRefreshListener(new OnRefreshListener() {

				@Override
				public void onRefresh() {
					new AsyncTask<Void, Void, Void>() {
						@Override
						protected Void doInBackground(Void... params) {
							try {
								Thread.sleep(2000);
								InitDatasDaoImpl
										.getConfirmOrderDatas(getActivity());
							} catch (Exception e) {
								e.printStackTrace();
							}
							return null;
						}

						@Override
						protected void onPostExecute(Void result) {
							adapter.notifyDataSetChanged();
							listView2.onRefreshComplete();
						}
					}.execute(null, null, null);
				}
			});
		} catch (Exception e) {
		}
	}

	public final class ViewHolder {
		private TextView shipperTv, huowuTv, hangxianTv, codeTv, yunliangTv,
				jiezhiTv, statustap;
		private LinearLayout linear2;
	}

	public class MyAdapter extends BaseAdapter {

		private final LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@SuppressLint({ "NewApi", "ResourceAsColor" })
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			ViewHolder holder = null;

			holder = new ViewHolder();
			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.confirm_order_listitem, null);
				holder.shipperTv = (TextView) convertView
						.findViewById(R.id.shippertv);
				holder.huowuTv = (TextView) convertView
						.findViewById(R.id.huowutv);
				holder.hangxianTv = (TextView) convertView
						.findViewById(R.id.hangxiantv);
				holder.codeTv = (TextView) convertView
						.findViewById(R.id.codetv);
				holder.yunliangTv = (TextView) convertView
						.findViewById(R.id.yunliangtv);
				holder.jiezhiTv = (TextView) convertView
						.findViewById(R.id.jiezhiriqitv);
				holder.statustap = (TextView) convertView
						.findViewById(R.id.statustap);
				holder.linear2 = (LinearLayout) convertView
						.findViewById(R.id.linear2);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			try {
				// 另item颜色分层
				if (position % 2 == 1) {
					holder.linear2.setBackgroundColor(getResources().getColor(
							R.color.light_gray2));
				} else {
					holder.linear2.setBackgroundColor(getResources().getColor(
							R.color.light_gray));
				}
			} catch (Exception e) {
			}
			holder.shipperTv.setText(mData.get(position).get("getShipper")
					.toString());
			holder.huowuTv.setText(mData.get(position).get("getGoods")
					.toString());
			holder.hangxianTv.setText(mData.get(position).get("getStartPort")
					.toString()
					+ "-" + mData.get(position).get("getEndPort").toString());
			holder.codeTv
					.setText(mData.get(position).get("getCode").toString());
			holder.yunliangTv.setText(mData.get(position).get("getGoodsCount")
					.toString()
					+ mData.get(position).get("getGoodsUnit").toString());
			holder.jiezhiTv.setText(mData.get(position).get("getClossingDate")
					.toString());
			if ("true".equals(mData.get(position).get("getShipperConfirm")
					.toString())) {
				holder.statustap.setText("您已确认");
				holder.statustap.setTextColor(getResources().getColor(
						R.color.already_con));
			} else {
				holder.statustap.setText("您未确认");
				holder.statustap.setTextColor(getResources().getColor(
						R.color.wait_con));
			}
			holder.linear2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					FunctionOfActivity.progressDialogShow(getActivity());
					// TODO Auto-generated method stub
					Intent intent = new Intent(getActivity(),
							ConfirmOrderActivity.class);
					intent.putExtra("getId", mData.get(position).get("getId")
							.toString());
					intent.putExtra("getCode",
							mData.get(position).get("getCode").toString());
					intent.putExtra("getGoodsUnit",
							mData.get(position).get("getGoodsUnit").toString());
					intent.putExtra("getLoadingPort",
							mData.get(position).get("getLoadingPort")
									.toString());
					intent.putExtra("getDischargePort", mData.get(position)
							.get("getDischargePort").toString());
					intent.putExtra("getLoadingCount",
							mData.get(position).get("getLoadingCount")
									.toString());
					intent.putExtra("getDischarge_Count", mData.get(position)
							.get("getDischarge_Count").toString());
					intent.putExtra("getShipperConfirm", mData.get(position)
							.get("getShipperConfirm").toString());
					intent.putExtra("getFinish",
							mData.get(position).get("getFinish").toString());
					intent.putExtra("getShipperConfirmAmount",
							mData.get(position).get("getShipperConfirmAmount")
									.toString());
					startActivity(intent);
				}
			});
			return convertView;
		}
	}

}
